//
//  MYXRecommendDetailController.m
//  meiyinxiang
//
//  Created by 媒印象 on 2018/1/11.
//  Copyright © 2018年 hbjApple. All rights reserved.
//
#import <HexColor.h>
#import "MYXRecommendDetailController.h"
#import "CommendListViewModel.h"
#import "CommentViewModel.h"
#import "CommendDetailSectionView.h"
#import "CommendDetailListCell.h"
@interface MYXRecommendDetailController ()<UITableViewDataSource,UITableViewDelegate>
{
    BOOL isAddMoreInfo;//是否增加评论了
}
@property (nonatomic,strong) CommendListViewModel *commentListViewModel;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation MYXRecommendDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    isAddMoreInfo = NO;
  //  self.title = @"回复详情";
    [self addCustomerNavBarWithTitle:@"回复详情"];
    self.commentListViewModel.mpage = 1;
    //请求数据
    [self loadData];
    [self configTableView];
    [self addmjrefresh];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;

}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.commentListViewModel.commentList.count >0 ) {
        CommentViewModel *viewmodel = self.commentListViewModel.commentList[0];
        [viewmodel hidekeybord];
    }
   
 
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    if (isAddMoreInfo == YES) {
        [[NSNotificationCenter defaultCenter] postNotificationName:Post5SecondSucessNotificationName object:nil];
    }
}
#pragma mark -PrivateMethod
- (void)loadData{
    [self.commentListViewModel getRecommendListIsPullup:NO WithRedPacketId:self.redPacketId commendManId:self.commentManId Result:^(BOOL isSucess) {
        [self.tableView reloadData];
        if (isSucess == YES) {
            [self.tableView.mj_footer endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
    }];

}
- (void)addmjrefresh{
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshMore)];
}
- (void)refreshMore{
    [self.commentListViewModel getRecommendListIsPullup:YES WithRedPacketId:self.redPacketId commendManId:self.commentManId Result:^(BOOL isSucess) {
        [self.tableView reloadData];
        if (isSucess == YES) {
            [self.tableView.mj_footer endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];

}
- (void)configTableView{
    [self.tableView registerNib:[UINib nibWithNibName:@"CommendDetailListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CommentListReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:@"CommendDetailSectionView" bundle:[NSBundle mainBundle]] forHeaderFooterViewReuseIdentifier:CommentDetailSecontionViewReuseId];

    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    AdjustsScrollViewInsetNever(self, self.tableView);
    [self.view addSubview:self.tableView];
}
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, ScreenWidth, ScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"F2F2F2"];
    }
    return _tableView;
}
- (CommendListViewModel *)commentListViewModel{
    if (_commentListViewModel == nil) {
        _commentListViewModel = [[CommendListViewModel alloc] init];
    }
    return _commentListViewModel;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return (self.commentListViewModel.commentList.count - 1 > 0) ? self.commentListViewModel.commentList.count - 1 : 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommendDetailListCell *commentListCell = [tableView dequeueReusableCellWithIdentifier:CommentListReuseId forIndexPath:indexPath];
    if (commentListCell == nil) {
        commentListCell = [CommendDetailListCell loadNib];
    }
    NSInteger mrow = indexPath.row + 1;
    CommentViewModel *viewModel = self.commentListViewModel.commentList[mrow];
    commentListCell.commentViewModel = viewModel;
    return commentListCell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CommendDetailSectionView *sectionView = [CommendDetailSectionView loadNib];
    if (self.commentListViewModel.commentList.count > 0) {
         sectionView.viewModel = self.commentListViewModel.commentList[0];
        WS(weakSelf);
        sectionView.viewModel.finishmsgBlock = ^(BOOL isSucess) {
            if (isSucess) {
                [weakSelf showSuccessTopMessage:@"评论成功"];
                isAddMoreInfo = YES;
                [weakSelf loadData];
            }
        };
        return sectionView;
    }
    
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 95;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentViewModel *viewmodel = self.commentListViewModel.commentList[0];
    [viewmodel hidekeybord];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.commentListViewModel.commentList.count > 0) {
        CommentViewModel *viewmodel = self.commentListViewModel.commentList[0];
        [viewmodel hidekeybord];
    }
   
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
